using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum PlayerBloodStatues
{
    FullHP,
    HP,
    LessHP
}
public class PlayerController : MonoBehaviour
{
    [SerializeField] private PlayerData provisionalData;                 // 临时数据
    [SerializeField] private PlayerData playerSaveData;                  // 存档
    [SerializeField] private PlayerData playerData;                      // 创建存档数据，读取之前存档的数据
    [SerializeField] private PlayerView playerView;
    private void Start()
    {
        playerView = GetComponent<PlayerView>();                             //获取视图脚本

        PlayerModel.Instance.InfoInitialize(playerData.playerBlood, playerData.playerScore);

        playerView.UpdateViewInfo(PlayerModel.Instance);                     //更新数据,初始化

        PlayerModel.Instance.AddEventListener(UpdateInfo);
        
        // 旧场景数据存储
        EventCenter.Instance.AddEventListener(EventName.EVENT_SAVEDATA_NEXTLEVEL, Save);
        // 新场景数据存储
        EventCenter.Instance.AddEventListener(EventName.EVENT_SAVEDATA_LOADNEWSCENE, SaveData);
        // 玩家复活读取数据
        EventCenter.Instance.AddEventListener<bool>(EventName.EVENT_GAME_RESTART, PlayerDataInitialization);
    }
    
    /// <summary>
    /// 增加血量
    /// </summary>
    public void AddPlayerBlood()
    {
        PlayerModel.Instance.AddPlayerBloodUpdate();
    }
    
    /// <summary>
    /// 减少血量
    /// </summary>
    public void ReducePlayerBlood()
    {
        PlayerModel.Instance.ReducePlayerBloodUpdate();print(PlayerModel.Instance.GetPlayerBlood);
    }
    
    /// <summary>
    /// 当前血量
    /// </summary>
    /// <returns></returns>
    public PlayerBloodStatues GetPlayerBloodStatus()
    {
        switch (PlayerModel.Instance.GetPlayerBlood)
        {
            case <=1:
                return PlayerBloodStatues.LessHP;
            case >=4:
                return PlayerBloodStatues.FullHP;
            default: return PlayerBloodStatues.HP;
        }
    }
    
    // 
    public void TakePlayerDeath()
    {
        PlayerModel.Instance.TakePlayerDeath();
    }
    
    /// <summary>
    /// 得分函数
    /// </summary>
    /// <param name="score">分数</param>
    public void AddPlayerScore(float score)
    {
        PlayerModel.Instance.PlayerScoreUpdate(score);
    }
    
    /// <summary>
    /// 视图界面更新
    /// </summary>
    /// <param name="instance">Model层数据</param>
    private void UpdateInfo(PlayerModel instance)
    {
        if(playerView != null)
        {
            playerView.UpdateViewInfo(instance);
        }
        // Debug.Log("更新数据 控制器 事件调用");
    }
    
    //
    private void SaveData()
    {
        // 把临时数据(新场景数据)转存到 存档数据中
        playerSaveData.playerPosition = provisionalData.playerPosition;
        playerSaveData.sceneIndex = provisionalData.sceneIndex;
        // 存档
        SaveDataSystem.SaveByJson(playerSaveData.playerName, playerSaveData);
    }
    
    //
    private void Save()
    {
        // 保存得分和血量
        print("玩家数据开始保存");
        playerSaveData.playerBlood = PlayerModel.Instance.GetPlayerBlood;
        playerSaveData.playerScore = PlayerModel.Instance.GetPlayerScore;
        SaveDataSystem.SaveByJson(playerSaveData.playerName, playerSaveData);
    }
    
    /// <summary>
    /// 玩家存档
    /// </summary>
    public void PlayerSaveData(Vector3 position, int sceneIndex)
    {
        print("玩家数据开始保存");
        playerSaveData.playerName = playerData.playerName;
        playerSaveData.playerBlood = PlayerModel.Instance.GetPlayerBlood;
        playerSaveData.playerScore = PlayerModel.Instance.GetPlayerScore;
        playerSaveData.playerPosition = position;
        playerSaveData.sceneIndex = sceneIndex;
        SaveDataSystem.SaveByJson(playerSaveData.playerName, playerSaveData);
    }
    
    /// <summary>
    /// 玩家死亡重新开始游戏时进行原有数据读档
    /// </summary>
    public void PlayerDataInitialization(bool value)
    {
        // 读取玩家存档数据
        PlayerModel.Instance.InfoInitialize(playerSaveData.playerBlood, playerSaveData.playerScore);
        // 
        this.gameObject.transform.position = playerSaveData.playerPosition;
    }
}
